//面版编辑
yutel.mod.ExtEditWindow = Ext.extend(Ext.Window, {
	extId :1,
	initDate : function(extEditForm){
			ajaxRequest('extView.action', {
				id:this.extId
			}, extEditForm, function(result) {
				var fieldArr = extEditForm.findByType('textfield');
				for (var i = 0; i < fieldArr.length; i++) {
					var targetName = fieldArr[i].getName();
					var names=targetName.split("\.");
					var targetValue = result.ext[names[1]];
					fieldArr[i].setValue(targetValue);
				}
				fieldArr = extEditForm.findByType('numberfield');
				for (var i = 0; i < fieldArr.length; i++) {
					var targetName = fieldArr[i].getName();
					var names=targetName.split("\.");
					var targetValue = result.ext[names[1]];
					fieldArr[i].setValue(targetValue);
				}
				fieldArr = extEditForm.findByType('combo');
				for (var i = 0; i < fieldArr.length; i++) {
					var targetName = fieldArr[i].getName();
					var names=targetName.split("\.");
					var targetValue = result.ext[names[1]];
					fieldArr[i].setValue(targetValue);
				}
			});
	},
	initComponent : function() {
		var tableStore = new Ext.data.Store({
			baseParams: {limit: 20},
			autoload: false,
			url: 'listTable.action',
			reader: new Ext.data.JsonReader({
					root: 'tableList',
					totalProperty: 'totalCount'
				},
				[
					{name: 'tableId', type: 'string', mapping: 'tableId'},
					{name: 'tableType', type: 'string', mapping: 'tableType'},
					{name: 'tableName', type: 'string', mapping: 'tableName'},
					{name: 'tableBusinessName', type: 'string', mapping: 'tableBusinessName'}
				])
		});
		var extEditForm = new Ext.FormPanel({
			frame : true,
			border : true,
			labelAlign : 'right',
			labelWidth : 100,
			url : 'extSave.action',
			onSubmit : Ext.emptyFn,
			defaultType : 'textfield',
			buttonAlign : 'center',
			bodyStyle : 'padding: 20 25;',
			minButtonWidth : 60,
			items : [{
						fieldLabel : '面版ID',
						xtype : 'numberfield',
						vtype : 'trimblank',
						readOnly : true,
						width : 150,
						name : 'ext.id'
					},{
						fieldLabel : '面版父ID',
						xtype : 'numberfield',
						vtype : 'trimblank',
						inputType : 'hidden',
						maxLength : 10,
						name : 'ext.pid',
						value : this.extId
					},{
						fieldLabel : '面版名称',
						vtype : 'trimblank',
						maxLength : 25,
						allowBlank : false,
						width : 150,
						name : 'ext.name'
					}, {
						fieldLabel : '面版类型',
						vtype : 'trimblank',
						maxLength : 25,
						width : 150,
						name : 'ext.type'
					},{
						fieldLabel : '面版宽度',
						xtype : 'numberfield',
						vtype : 'trimblank',
						maxLength : 5,
						width : 150,
						name : 'ext.width'
					},{
						fieldLabel : '面版高度',
						xtype : 'numberfield',
						vtype : 'trimblank',
						maxLength : 5,
						width : 150,
						name : 'ext.height'
					}, {
						fieldLabel : '布局',
						vtype : 'trimblank',
						maxLength : 50,
						width : 150,
						name : 'ext.layout'
					},{
						fieldLabel : '方位',
						vtype : 'trimblank',
						maxLength : 15,
						width : 150,
						name : 'ext.region'
					},{
						xtype : 'combo',
						fieldLabel : '数据表',
						maxLength : 20,
						name : 'ext.tableName',
						emptyText : '请选择',
						valueField : 'tableId',
						displayField : 'tableName',
						triggerAction : 'all',
						width : 150,
						mode : 'remote',
						store :tableStore,
						selectOnFocus : true,
						listeners : {
						  'select':function(combo, record, index) {
							this.ownerCt.getForm()
									.findField('ext.tableId')
									.setValue(combo.getValue());
						  }
						}
					},{
						inputType : 'hidden',
						name : 'ext.tableId'
					},{
						fieldLabel : '排序',
						xtype : 'numberfield',
						vtype : 'trimblank',
						maxLength : 2,
						width : 150,
						name : 'ext.sort'
					}],
			buttons : [{
				text : '保存',
				handler : function(btn) {
					var wind = this.ownerCt;
					if(extEditForm.getForm().isValid()) {
						extEditForm.getForm().submit({
							success : function (extEditForm,action) {
								Ext.Msg.alert('成功',action.result.msg);
								Ext.getCmp('extjsPanel').items.itemAt(0).items.itemAt(0).root.reload();
								wind.ownerCt.hide();
							},
							failure : function (userEditForm,action) {
								Ext.Msg.alert('失败',action.result.msg);
							}
						});
					} else {
						Ext.Msg.alert('提示','请检查（红线框）输入项是否符合要求！');
					}
				}
			},{
				text : '取消',
				handler : function() {
					this.ownerCt.ownerCt.hide();
				}}]
		});

		Ext.apply(this, {
					frame : true,
					border : true,
					title : '面版维护',
					width : 350,
					height : 350,
					resizable : false,
					modal : true,
					closeAction : 'hide',
					layout : 'fit',
					items : [extEditForm]
				});

		yutel.mod.ExtEditWindow.superclass.initComponent.call(this);
	}
});
//面版菜单
var extRightClick = new Ext.menu.Menu({
        extNode : {},
        items : [{
            id:'refashExt',
            text : '刷新面版',
            iconCls : 'icon-ref',
            handler:function (){
            	extRightClick.extNode.reload();
            }
        },{
            id:'addExt',
            text : '添加面版',
            iconCls : 'icon-add',
            //增加菜单点击事件
            handler:function (){
            	new yutel.mod.ExtEditWindow({extId:extRightClick.extNode.id}).show();
            }
         }, {
            id:'editExt',
            text : '编辑面版',
            iconCls : 'icon-edit',
            handler:function (){
				var extWin=new yutel.mod.ExtEditWindow({extId:extRightClick.extNode.id});
            	extWin.show();
            	extWin.initDate(extWin.items.itemAt(0));
            }
        }, {
            id:'delExt',
            text : '删除面版',
            iconCls : 'icon-del',
            handler:function (){
				Ext.Ajax.request({
					url : 'extDrop.action',
					params : {
						id:extRightClick.extNode.id
					},
					success : function(response) {
						var result = Ext.decode(response.responseText);
						Ext.Msg.alert("提示",result.msg);
						Ext.getCmp('extjsPanel').items.itemAt(0).items.itemAt(0).root.reload();
					},
					failure : function(response) {
						var result = Ext.decode(response.responseText);
						Ext.Msg.alert("提示",result.msg);
					},
					scope : this
            	});
            }
        }]
     });
//面版树
yutel.mod.ExtTree = Ext.extend(Ext.tree.TreePanel, {
	initComponent : function() {
		Ext.apply(this, {
					id : 'extTreeId',
					title : '面版树',
					width : 250,
					minSize : 200,
					maxSize : 250,
					border : true,
					frame : true,
					rootVisible : true,
					autoScroll : true,
					autoHeight : false,// 自动高
					containerScroll : true,
					root : new Ext.tree.AsyncTreeNode({
						id : '0',
						text : "面版树",
						qtip : '面版树',
						tableId :'0',
						expanded : true
					}),
					loader : new TonyTreeLoader({
						url : "extTree.action",
						listeners : {
							"beforeload" : function(treeLoader, node) {
								treeLoader.baseParams.id = node.id;
							}
						}
					})
				});
		yutel.mod.ExtTree.superclass.initComponent.call(this);
		this.addListener('click', function(node) {
			var listp=Ext.getCmp('extjsPanel').items.itemAt(0).items.itemAt(1);
			var tableId=node.attributes.tableId;
			if(tableId!=0){
				listp.getStore().baseParams={id:node.id};
				listp.getStore().load();
				listp.buttonEnable();
			} else {
				Ext.Msg.alert('提示','请先设置数据表！');
			}
		});
		this.addListener('contextmenu',function(node,event){
		  	extRightClick.extNode=node;
			event.preventDefault(); //这行是必须的 
			extRightClick.showAt(event.getXY());//取得鼠标点击坐标，展示菜单
		});
	}
});
//功能编辑
yutel.mod.CommEditWindow = Ext.extend(Ext.Window, {
	panelId :0,
	tableId :0,
	commId :0,
	initData : function(commEditForm){
			ajaxRequest('commView.action', {
				id:this.commId
			}, commEditForm, function(result) {
				var fieldArr = commEditForm.findByType('textfield');
				for (var i = 0; i < fieldArr.length; i++) {
					var targetName = fieldArr[i].getName();
					var names=targetName.split("\.");
					var targetValue = result.comm[names[1]];
					fieldArr[i].setValue(targetValue);
				}
				fieldArr = commEditForm.findByType('numberfield');
				for (var i = 0; i < fieldArr.length; i++) {
					var targetName = fieldArr[i].getName();
					var names=targetName.split("\.");
					var targetValue = result.comm[names[1]];
					fieldArr[i].setValue(targetValue);
				}
				fieldArr = commEditForm.findByType('combo');
				for (var i = 0; i < fieldArr.length; i++) {
					var targetName = fieldArr[i].getName();
					var names=targetName.split("\.");
					var targetValue = result.comm[names[1]];
					fieldArr[i].setValue(targetValue);
				}
			});
	},
	initComponent : function() {
		var columnStore = new Ext.data.Store({
			autoload: false,
			url: 'listTableColumn.action?tableId='+this.tableId,
			reader: new Ext.data.JsonReader({},
				[
				   {name: 'columnId', type: 'string'},
		           {name: 'fieldName', type: 'string'},
		           {name: 'businName', type: 'string'},
		           {name: 'fieldType', type: 'string'},
		           {name: 'nullAble', type: 'string'}
				])
		});
		var commEditForm = new Ext.FormPanel({
			frame : true,
			border : true,
			labelAlign : 'right',
			labelWidth : 100,
			url : 'commSave.action',
			onSubmit : Ext.emptyFn,
			defaultType : 'textfield',
			buttonAlign : 'center',
			bodyStyle : 'padding: 20 25;',
			minButtonWidth : 60,
			items : [{
						fieldLabel : '功能ID',
						xtype : 'numberfield',
						vtype : 'trimblank',
						inputType : 'hidden',
						maxLength : 10,
						name : 'comm.id',
						value : this.commId
					},{
						fieldLabel : '面版ID',
						xtype : 'numberfield',
						vtype : 'trimblank',
						inputType : 'hidden',
						maxLength : 10,
						name : 'comm.panelId',
						value :this.panelId
					},{
						fieldLabel : '功能类型',
						vtype : 'trimblank',
						maxLength : 25,
						allowBlank : false,
						width : 150,
						name : 'comm.type'
					}, {
						fieldLabel : '功能名称',
						vtype : 'trimblank',
						maxLength : 25,
						allowBlank : false,
						width :150,
						name : 'comm.name'
					},{
						xtype : 'combo',
						fieldLabel : '表列名称',
						vtype : 'trimblank',
						maxLength : 15,
						name : 'comm.colName',
						valueField : 'columnId',
						displayField : 'fieldName',
						triggerAction : 'all',
						width : 150,
						mode : 'remote',
						store :columnStore,
						selectOnFocus : true,
						listeners : {
						  'select':function(combo, record, index) {
							this.ownerCt.getForm().findField('comm.colId').setValue(combo.getValue());
							this.ownerCt.getForm().findField('comm.colType').setValue(record.get('fieldType'));
							this.ownerCt.getForm().findField('comm.colNullAble').setValue(record.get('nullAble'));
						  }
						}
					},{
						fieldLabel : '表列Id',
						xtype : 'numberfield',
						vtype : 'trimblank',
						maxLength : 5,
						width :150,
						name : 'comm.colId'
					},{
						fieldLabel : '表列类型',
						vtype : 'trimblank',
						maxLength : 30,
						width :150,
						name : 'comm.colType'
					},{
						fieldLabel : '充许为空',
						vtype : 'trimblank',
						maxLength : 2,
						width :150,
						name : 'comm.colNullAble'
					},{
						xtype : 'combo',
						fieldLabel : '是否隐藏',
						valueField : 'name',
						displayField : 'name',
						triggerAction : 'all',
						width :150,
						name : 'comm.hidden',
						mode : 'local',
						value : '否',
						store : new Ext.data.SimpleStore({
								fields : ['name', 'name'],
								data : [['是', '是'], ['否', '否']]
							}),
						selectOnFocus : true
					},{
						fieldLabel : '目标面版',
						xtype : 'numberfield',
						vtype : 'trimblank',
						maxLength : 5,
						width :150,
						name : 'comm.targetId'
					},{
						fieldLabel : '排序',
						xtype : 'numberfield',
						vtype : 'trimblank',
						maxLength : 2,
						width :150,
						name : 'comm.sort'
					}],
			buttons : [{
				text : '保存',
				handler : function(btn) {
					var wind = this.ownerCt;
					if(commEditForm.getForm().isValid()) {
						commEditForm.getForm().submit({
							success : function (commEditForm,action) {
								Ext.Msg.alert('成功',action.result.msg);
								Ext.getCmp('extjsPanel').items.itemAt(0).items.itemAt(1).getStore().reload();
								wind.ownerCt.hide();
							},
							failure : function (userEditForm,action) {
								Ext.Msg.alert('失败',action.result.msg);
							}
						});
					} else {
						Ext.Msg.alert('提示','请检查（红线框）输入项是否符合要求！');
					}
				}
			},{
				text : '取消',
				handler : function() {
					this.ownerCt.ownerCt.hide();
				}}]
		});

		Ext.apply(this, {
					frame : true,
					border : true,
					title : '功能维护',
					width : 350,
					height : 350,
					resizable : false,
					modal : true,
					closeAction : 'hide',
					layout : 'fit',
					items : [commEditForm]
				});

		yutel.mod.ExtEditWindow.superclass.initComponent.call(this);
	}
});
//功能列表
yutel.mod.ExtList = Ext.extend(Ext.grid.GridPanel, {
			buttonEnable: function(){
				this.getTopToolbar().enable();	
			},
			buttonDisable: function(){
				this.getTopToolbar().disable();	
			},
			initComponent : function() {
				// 新建功能
				var btn_new = new Ext.Button({
					text : '新建功能',
					iconCls : 'icon-add',
					disabled  : true,
					handler:function (){
						var extTreeP=Ext.getCmp('extjsPanel').findById('extTreeId');
						var selectNode = extTreeP.selModel.selNode;
						if (selectNode == null) {
							Ext.Msg.alert("提示信息",'请选择所属的面版！');
							return;
						}
		            	new yutel.mod.CommEditWindow({
		            		panelId:selectNode.id,
		            		tableId:selectNode.attributes.tableId
		            	}).show();
		            }
				});
				//编辑功能
				var btn_edit = new Ext.Button({
					text : '编辑功能',
					iconCls : 'icon-edit',
					disabled  : true,
					handler:function (){
						var extTreeP=Ext.getCmp('extjsPanel').findById('extTreeId');
						var selectNode = extTreeP.selModel.selNode;
						if (selectNode == null) {
							Ext.Msg.alert("提示信息",'请先选择所属的面版！');
							return;
						}
						var commListP = Ext.getCmp('extjsPanel').items.itemAt(0).items.itemAt(1);
						var selCount = commListP.getSelectionModel().getCount();
						if (selCount == 0) {
							Ext.Msg.alert("提示信息",'请选择要修改的功能！');
							return;
						}
		            	var commWin=new yutel.mod.CommEditWindow({
		            		commId:commListP.getSelectionModel().getSelected().get('id'),
		            		tableId:extTreeP.selModel.selNode.attributes.tableId
		            	});
		            	commWin.show();
		            	commWin.initData(commWin.items.itemAt(0));
		            }
				});
				// 删除功能
				var btn_del = new Ext.Button({
					text : '删除功能',
					iconCls : 'icon-del',
					disabled  : true,
					handler:function (){
						var extTreeP=Ext.getCmp('extjsPanel').items.itemAt(0).items.itemAt(0);
						var commListP = Ext.getCmp('extjsPanel').items.itemAt(0).items.itemAt(1);
						var selCount = commListP.getSelectionModel().getCount();
						if (selCount == 0) {
							Ext.Msg.alert("提示信息",'请选择要删除的功能！');
							return;
						} else {
							var commIds='';
							var records = commListP.getSelectionModel().getSelections();
							for (var i = 0; i < selCount; i++) {
								commIds=commIds+records[i].get('id')+",";
							}
							Ext.Msg.show({
								title : '确认删除',
								msg : "您确认要删除选择的功能吗？",
								buttons : Ext.Msg.YESNO,
								fn : function(bb) {
									if (bb == 'yes') {
										Ext.getBody().mask("正在删除数据,请稍候...","x-mask-loading");
										Ext.Ajax.request({
											url : 'commDrop.action',
											params : {
												commIds:commIds
											},
											success : function(response) {
												var result = Ext.decode(response.responseText);
												Ext.Msg.alert("成功提示",result.msg);
												Ext.getBody().unmask();
												commListP.getStore().reload();
											},
											failure : function(response) {
												Ext.Msg.alert("出错提示",result.msg);
											},
											scope : this
						            	});
									} else {
										return;
									}
								},
								scope : this,
								icon : Ext.MessageBox.QUESTION
							});
						}
		            }
				});
				// 零配件模板列模式定义
				var templateGridPanel = this;
				var cm = new Ext.grid.ColumnModel([new Ext.grid.CheckboxSelectionModel(),
						{
							header : '面版ID',
							dataIndex : 'panelId',
							sortable : true,
							hidden : true
						}, {
							header : '功能ID',
							dataIndex : 'id',
							sortable : true,
							hidden : true
						}, {
							header : '功能类型',
							dataIndex : 'type',
							sortable : true
						}, {
							header : '功能名称',
							dataIndex : 'name',
							sortable : true
						},{
							header : '表列Id',
							dataIndex : 'colId',
							sortable : true
						},{
							header : '表列名称',
							dataIndex : 'colName',
							sortable : true
						},{
							header : '表列类型',
							dataIndex : 'colType',
							sortable : true
						},{
							header : '充许为空',
							dataIndex : 'colNullAble',
							sortable : true
						},{
							header : '是否隐藏',
							dataIndex : 'hidden',
							sortable : true
						},{
							header : '目标面版',
							dataIndex : 'targetId',
							sortable : true
						},{
							header : '排序',
							dataIndex : 'sort',
							sortable : true
						}]);

				var ds = new Ext.data.Store({
							autoLoad : false,
							proxy : new Ext.data.HttpProxy({
										url : 'extComm.action'
									}),
							reader : new Ext.data.JsonReader({
										totalProperty : 'totalProperty',
										root : 'root'
									}, [{
												name : 'panelId',
												type : 'int'
										},{
												name : 'id',
												type : 'int'
										},{
												name : 'type',
												type : 'string'
										},{
												name : 'name',
												type : 'string'
										},{
												name : 'colId',
												type : 'int'
										},{
												name : 'colName',
												type : 'string'
										},{
												name : 'colType',
												type : 'string'
										},{
												name : 'colNullAble',
												type : 'string'
										},{
												name : 'hidden',
												type : 'string'
										},{
												name : 'targetId',
												type : 'int'
										},{
												name : 'sort',
												type : 'int'
										}])
						});
				Ext.apply(this, {
							title : '功能列表',
							border : true,
							frame : true,
							autoScroll : true,
							autoHeight : false,
							bodyStyle : 'width:100%',
							loadMask : {
								msg : '数据加载中...'
							},
							viewConfig : {
								forceFit : true
							},
							tbar :[btn_new,btn_edit,btn_del],
							cm : cm,
							ds : ds,
							sm : new Ext.grid.CheckboxSelectionModel({
										singleSelect : false
									}),
							enableColumnMove : false,
							clicksToEdit : 1
						});
				yutel.mod.ExtList.superclass.initComponent.call(this);
			}
});
//主面版
yutel.mod.ExtMainPanel = Ext.extend(Ext.Panel, {
	initComponent : function() {
		var extTree = new yutel.mod.ExtTree({region : 'west'});
		var extList = new yutel.mod.ExtList({region : 'center'});
		Ext.apply(this, {
					layout : 'border',
						items : [extTree,extList]
					});
		yutel.mod.ExtMainPanel.superclass.initComponent.call(this);
	}
});
Ext.reg('extjsPanel', yutel.mod.ExtMainPanel);